package cn.edu.xmu.oomall.liquidation.model.bo.liquid.expenditure;

import cn.edu.xmu.oomall.liquidation.dao.LiquidationDao;
import cn.edu.xmu.oomall.liquidation.microservice.AfterSaleService;
import cn.edu.xmu.oomall.liquidation.microservice.OrderService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.AfterSaleRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.OrderItemRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.RefundRetVo;
import cn.edu.xmu.oomall.liquidation.model.bo.ExpenditureItem;
import cn.edu.xmu.oomall.liquidation.model.bo.Liquidation;
import cn.edu.xmu.oomall.liquidation.model.bo.RevenueItem;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.CalcTotal;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.LiquidationCalc;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.RefundCalcInfo;
import cn.edu.xmu.oomall.liquidation.model.po.RevenueItemPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Component
public class AfterSaleRefundLiquid extends BaseRefundLiquid {
    @Autowired
    private AfterSaleService afterSaleService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private LiquidationCalc liquidationCalc;

    @Override
    protected CalcTotal liquidateSingleRefund(RefundRetVo refund, LocalDateTime liquidDateTime) {
        CalcTotal platformCalcTotal = new CalcTotal(liquidUtil.getPlatformCalcFieldList());
        String afterSaleSn = refund.getDocumentId();
        InternalReturnObject<AfterSaleRetVo> afterSaleObj = afterSaleService.getAfterSaleBySn(afterSaleSn);
        AfterSaleRetVo afterSale = afterSaleObj.getData();

        // 获得清算单
        Liquidation liquid = liquidUtil.getLiquid(afterSale.getShopId(), liquidDateTime);
        Long liquidId = liquid.getId();

        // 进行售后清算
        CalcTotal calcTotal = liquidateAfterSale(afterSale, liquidId, refund);

        // 更新商铺清算单
        calcTotal.changeLiquid(liquid);
        liquidUtil.updateLiquid(liquid);

        //更新平台统计
        platformCalcTotal.add(calcTotal);

        return platformCalcTotal;
    }

    private CalcTotal liquidateAfterSale(AfterSaleRetVo afterSale, Long liquidId, RefundRetVo refund) {
        CalcTotal calcTotal = new CalcTotal(liquidUtil.getShopCalcFieldList());
        Long orderItemId = afterSale.getOrderItemId();
        OrderItemRetVo orderItem = orderService.getOrderItemById(orderItemId).getData();

        // 查询该orderItem对应的进账单
        RevenueItemPoExample revenueItemPoExample = new RevenueItemPoExample();
        RevenueItemPoExample.Criteria criteria = revenueItemPoExample.createCriteria();
        criteria.andPaymentIdEqualTo(refund.getPaymentId());
        criteria.andOrderitemIdEqualTo(orderItemId);
        List<RevenueItem> revenueItemList = (List<RevenueItem>) liquidUtil.getRevenueItem(revenueItemPoExample).getData();
        RevenueItem revenueItem = revenueItemList.get(0);

        // 生成出账单
        ExpenditureItem expenditureItem = new ExpenditureItem();
        copyFromRevenue(expenditureItem, revenueItem);
        expenditureItem.setLiquidId(liquidId);
        expenditureItem.setRefundId(refund.getId());
        expenditureItem.setAmount(-refund.getAmount());
        expenditureItem.setQuantity(afterSale.getQuantity());
        expenditureItem.setExpressFee(0L);
        expenditureItem.setShopRevenue(expenditureItem.getAmount());

        RefundCalcInfo calcInfo = new RefundCalcInfo(orderItem.getProductId(), revenueItem.getSharerId(),
                orderItem.getOnsaleId(), orderItem.getQuantity(), orderItemId, expenditureItem);
        Map<String, Long> calcMap = liquidationCalc.calc(calcInfo);
        // 插入数据库
        liquidUtil.insertExpenditure(expenditureItem);
        //更新统计信息
        calcMap.put("shopRevenue", expenditureItem.getShopRevenue());
        calcTotal.add(calcMap);

        return calcTotal;
    }

    private void copyFromRevenue(ExpenditureItem expenditureItem, RevenueItem revenueItem) {
        expenditureItem.setShopId(revenueItem.getShopId());
        expenditureItem.setRevenueId(revenueItem.getId());
        expenditureItem.setOrderId(revenueItem.getOrderId());
        expenditureItem.setOrderitemId(revenueItem.getOrderitemId());
        expenditureItem.setProductId(revenueItem.getProductId());
        expenditureItem.setProductName(revenueItem.getProductName());
        expenditureItem.setSharerId(revenueItem.getSharerId());
    }
}
